package com.vichat.gps.controller;

import com.alibaba.fastjson.JSONObject;
import com.vichat.common.cache.RedisHelper;
import com.vichat.common.constant.GlobalConstant;
import com.vichat.common.util.DateUtil;
import com.vichat.common.vo.Json;
import com.vichat.gps.entity.LocationData;
import com.vichat.gps.entity.LocationDataLog;
import com.vichat.gps.service.IGpsService;
import com.vichat.user.controller.BaseController;
import com.vichat.user.entity.UserBasic;
import com.vichat.user.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by ranjx on 2017/11/20.
 */
@Controller
@RequestMapping("/vcapi/gps")
public class GpsController extends BaseController {
    @Autowired
    HttpServletRequest request;
    @Autowired
    HttpServletResponse response;

    @Autowired
    private IGpsService gpsService;
    @Autowired
    private IUserService userService;

    /**
     * 调度地图
     *
     * @return
     */
    @RequestMapping("/mainMap")
    public String mainMap() {
        return "/gps/mainMap";
    }

    /**
     * 获取所有人员当天最新定位信息
     *
     * @return
     */
    @RequestMapping("/getUserLocations")
    @ResponseBody
    public Json getUserLocations(Date startDate, Date endDate) {
        Json json = new Json();
        try {
            UserBasic userBasic = getSessionUser();
            LocationData queryBean = new LocationData();
            queryBean.setPartyId(userBasic.getPartyId());
            queryBean.setOrgCode(userBasic.getOrgCode());
            List<LocationDataLog> locationDatas = gpsService.queryLocationDatas(queryBean, startDate, endDate);
            if (!locationDatas.isEmpty()) {
                for (LocationDataLog val : locationDatas) {
                    UserBasic userFDB = userService.getUserBasicByUid(val.getUid());
                    if (userFDB != null) {
                        val.setUserName(userFDB.getUserName());
                        val.setTel(userFDB.getPhone());
                    }
                }
            }
            json.setObj(locationDatas);
            json.setSuccess(true);
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            json.setCode(-9);
        }
        return json;
    }


    /**
     * 获取用户gps定位轨迹
     *
     * @param uid
     * @param date
     * @return
     */
    @RequestMapping("/getLocationsByUid")
    @ResponseBody
    public Json getLocationsByUid(@RequestParam("uid") Long uid, @RequestParam("date") String date) {
        Json json = new Json();
        try {
            UserBasic sessionUser = getSessionUser();
            UserBasic user = userService.getUserBasicByUid(uid);
            //查找所在区域
            if (user != null && user.getPartyId().longValue() == sessionUser.getPartyId().longValue()) {
                List<LocationData> locationDatas = gpsService.getLocationDatasByUid(uid, date);
                if (!locationDatas.isEmpty()) {
                    for (LocationData val : locationDatas) {
                        UserBasic userFDB = userService.getUserBasicByUid(val.getUid());
                        if (userFDB != null) {
                            val.setUserName(userFDB.getUserName());
                            val.setTel(userFDB.getPhone());
                        }
                    }
                }
                json.setObj(locationDatas);
                json.setSuccess(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setCode(-9);
        }
        return json;
    }


    /**
     * 查询勾选的人员定位
     *
     * @return
     */
    @RequestMapping("/getSelectedUserLocation")
    @ResponseBody
    public Json getSelectedUserLocation(@RequestParam(value = "uids") Long[] uids, boolean isToday) {
        Json json = new Json();
        try {
            if (uids.length > 0) {
                List<LocationData> selectLocations = new ArrayList<>();
                Date today = new Date();
                for (long uid : uids) {
                    String locationDataJson = RedisHelper.getUserLocationData(uid);
                    if (StringUtils.isNotBlank(locationDataJson)) {
                        LocationData locationData = JSONObject.parseObject(locationDataJson, LocationData.class);

                        UserBasic userFDB = userService.getUserBasicByUid(uid);
                        if (userFDB != null) {
                            locationData.setUserName(userFDB.getUserName());
                            locationData.setTel(userFDB.getPhone());
                        }

                        if (isToday && DateUtil.isSameDate(new Date(locationData.getLocTime()), today)) {// 非当天的不提供
                            selectLocations.add(locationData);
                        } else {
                            selectLocations.add(locationData);
                        }
                    }
                }
                json.setObj(selectLocations);
                json.setSuccess(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
        }
        return json;
    }

    /**
     * 查询在线人员定位
     *
     * @return
     */
    @RequestMapping("/getOnlineLocation")
    @ResponseBody
    public Json getOnlineLocation() {
        Json json = new Json();
        try {
            UserBasic sessionUser = getSessionUser();
            long partyId = sessionUser.getPartyId();
            UserBasic queryBean = new UserBasic();
            queryBean.setPartyId(partyId);
            queryBean.setOrgCode(sessionUser.getOrgCode());
            queryBean.setDisplayState(GlobalConstant.DISPLAY_STATE_1);
            List<UserBasic> onlineUsers = userService.getOnlineUsers(queryBean);
            List<LocationData> onlineLocations = new ArrayList<>();
            if (!onlineUsers.isEmpty()) {
                Date today = new Date();
                for (UserBasic val : onlineUsers) {
                    String locationDataJson = RedisHelper.getUserLocationData(val.getUid());
                    if (StringUtils.isNotBlank(locationDataJson)) {
                        LocationData locationData = JSONObject.parseObject(locationDataJson, LocationData.class);
                        if (DateUtil.isSameDate(new Date(locationData.getLocTime()), today)) {// 非当天的不提供
                            locationData.setUserName(val.getUserName());
//                            locationData.setTrafficTool(sessionUser.getExt1());
//                            locationData.setHasavatar(val.getHasavatar());
                            onlineLocations.add(locationData);
                        }
                    }
                }
                json.setObj(onlineLocations);
            }
            json.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
        }
        return json;
    }
}
